<template>
  <div class="chart-container">
    <div id="chartDriver" class="chart"></div>
    <div id="chartGoods" class="chart"></div>
  </div>
</template>

<script setup>
import * as echarts from "echarts";
import { onMounted } from "vue";
import { ref } from 'vue';
import userApi from "@/api/userApi.js";
import goodsApi from "@/api/goodsApi.js";

const driverName = ref([]);
const sum = ref([]);
const goodsName = ref([]);
const hsum = ref([]);

function initChartDriver() {
  var chartDom = document.getElementById('chartDriver');
  var myChart = echarts.init(chartDom);
  var option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    legend: {
      top: '5%',
      left: 'center',
      data: driverName.value
    },
    series: [
      {
        name: '销售数量',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        padAngle: 5,
        itemStyle: {
          borderRadius: 10
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '40',
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: driverName.value.map((name, index) => ({ value: sum.value[index], name }))
      }
    ]
  };
  myChart.setOption(option);
}

function initChartGoods() {
  var chartDom = document.getElementById('chartGoods');
  var myChart = echarts.init(chartDom);
  var option = {
    title: {
      text: '订单数量前5用户',
      left: 'center',
      top: '20px',
      textStyle: {
        color: '#047',
        fontSize: 24,
        fontWeight: 'normal'
      }
    },
    xAxis: {
      type: 'category',
      data: goodsName.value,
      axisLabel: {
        rotate: 45,
        interval: 0
      }
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: hsum.value,
        type: 'bar',
        showBackground: true,
        backgroundStyle: {
          color: 'rgba(100, 190, 180, 0.2)'
        }
      }
    ]
  };
  myChart.setOption(option);
}

function top5() {
  goodsApi.goodsTop5()
      .then(resp => {
        driverName.value = resp.data.map(item => item.name);
        sum.value = resp.data.map(item => item.total_quantity);
        initChartDriver();
      })
      .catch(error => {
        console.error("获取数据失败", error);
      });
}

function top5salary() {
  userApi.userTop5()
      .then(resp => {
        goodsName.value = resp.data.map(item => item.realname);
        hsum.value = resp.data.map(item => item.total);
        initChartGoods();
      })
      .catch(error => {
        console.error("获取数据失败", error);
      });
}

onMounted(() => {
  top5();
  top5salary();
});
</script>

<style scoped>
.chart-container {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 100%;
}

.chart {
  width: 70vw;
  /* 或者其他宽度 */
  height: 80vh;
  /* 或者其他高度 */
}
</style>